require(PBSadmb)
source("adfunctions.r")
setwd("R")

stmod=1
endmod=4
mod0<-readList("..\\arc\\mod0_r.dat")
mod1<-readList("..\\arc\\mod1_r.dat")
mod2<-readList("..\\arc\\mod2_r.dat")
mod3<-readList("..\\arc\\mod3_r.dat")
mod4<-readList("..\\arc\\mod4_r.dat")
#mod5<-readList("..\\arc\\mod5_r.dat")
#mod6<-readList("..\\arc\\mod6_r.dat")
#mod7<-readList("..\\arc\\mod7_r.dat")
#mod8<-readList("..\\arc\\mod8_r.dat")

pdf("..\\results\\allfigs.pdf",width=9, height=7)
for (i in stmod:endmod){
  y<-paste("mod",i,sep="")
  print(y)
  mod<-get(y)
  p.rec.hist(mod,main=paste("Model",i),ylab="Age 2 recruitment",fy=1970,ly=2010)
}
p.rec.hist(mod1,main=paste("Model",1),ylab="Age 2 recruitment",fy=1970,ly=2011)

for (i in stmod:endmod){
  y<-paste("mod",i,sep="")
  print(y)
  mod<-get(y)
  p.biom.pol(mod,typ="SSB",main=paste("Model",i),new=F)
}

for (i in stmod:endmod){
  y<-paste("mod",i,sep="")
  #print(y)
  mod<-get(y)
  Mntns(mod,paste("Model",i))
}
for (i in stmod:endmod){
  y<-paste("mod",i,sep="")
  print(y)
  mod<-get(y)
  AgeFits(mod,main=paste("Model",i),f=1)
}


for (i in stmod:endmod){
  y<-paste("mod",i,sep="")
  print(y)
  mod<-get(y)
  AgeFits(mod,main=paste("Model",i),f=2)
}

for (i in stmod:endmod){
  y<-paste("mod",i,sep="")
  print(y)
  mod<-get(y)
  AgeFits(mod,main=paste("Model",i),f=3)
}

for (i in stmod:endmod){
  y<-paste("mod",i,sep="")
  print(y)
  mod<-get(y)
  AgeFits(mod,main=paste("Model",i),f=4)
}
par(mfrow=c(1,1))
#for (i in 1:7){ y<-paste("mod",i,sep="") print(y) mod<-get(y) spwn_ratio(mod,paste("Model",i)) }
for (i in stmod:endmod){
  y<-paste("mod",i,sep="")
  print(y)
  mod<-get(y)
  p.stock.rec(mod,main=paste("Model",i))
}

dev.off()

pdf("..\\results\\Srec.pdf",width=9, height=7)
for (i in stmod:endmod){
  y<-paste("mod",i,sep="")
  print(y)
  mod<-get(y)
  p.stock.rec(mod,main=paste("Model",i))
}

dev.off()
names(mod1)

p.stock.rec(mod1,main=paste("Model",1))
pdf("..\\results\\sel_srv.pdf",width=9, height=7)
  Mntns_srv(mod1,"Model 1")
  Mntns_srv2(mod1,"Model 1")
  dev.off()
  
  Likes=matrix(nrow=endmod,ncol=12,)
for (i in stmod:4){
  y<-paste("mod",i,sep="")
  mod<-get(y)
  Likes[i,]=mod$Like_Comp
}
write.csv(Likes,"Likes.csv")
shell("vi likes.csv")
pdf("..\\results\\compare.pdf",width=9, height=7)
  plot(mod1$SSB[12:54,1],mod1$SSB[12:54,2], xlab="Year",type="l",lwd=3,col="red",ylab="SSB",ylim=c(1.,22.e6))
  lines(mod2$SSB[12:54,1],mod2$SSB[12:54,2], lwd=3,col="blue")
  lines(mod3$SSB[12:54,1],mod3$SSB[12:54,2], lwd=3,col="green")
  lines(mod4$SSB[12:54,1],mod4$SSB[12:54,2], lwd=1,col="black")
  legend(1970,5e6,c("Mod_1","Mod_2","Mod_3","Mod_4"),lwd=c(3,3,3,1),col=c("red","blue","green","black"))
dev.off()
mod1$SSB
AgeFitsSrv(mod1,main="Model 1",f=1)

proj=read.table("clipboard",header=T)
names(proj)
proj$F

proj$Yr=as.factor(proj$Yr)
proj$Model=as.factor(proj$Model)
boxplot(SSB~Yr,data=proj[proj$C_scen==5,],col="salmon")  
boxplot(SSB~Yr,data=proj[proj$C_scen==5,],col="salmon",main="Catch=522 kt")
`?lattice
ptmp=proj[proj$Model==1&proj$C_Scen==3,]
names(ptmp)
ptmp$Yr
ptmp$Yr=as.factor(ptmp$Yr)
bwplot(SSB~Yr,data=ptmp)
d = ptmp[ptmp$Sim==33,]
lines(d$Yr,d$SSB)
aggregate(F~Yr+Model+C_Scen,data=proj,median)[,4                                            ]
psum=cbind(aggregate(SSB~Yr+Model+C_Scen,data=proj,median),aggregate(F~Yr+Model+C_Scen,data=proj,median)[,4],
           aggregate(SPR_Implied~Yr+Model+C_Scen,data=proj,median)[,4])
names(psum)

psum$F
colnames(psum)=c(colnames(psum)[1:4],"F","SPR")
psum$Yr=as.factor(psum$Yr)
psum$Model=as.factor(psum$Model)
psum$C_Scen=as.factor(psum$C_Scen)
xtabs(SSB~Yr+Model+C_Scen,data=psum)
xyplot(F~Yr|Model+C_Scen,data=psum,type="o")
write.csv(psum,"Median.csv")
shell("vi Median.csv")
boxplot(SSB~Yr+Model,data=proj[proj$Model==1,],col="salmon")
# Ratio of unfished
mod1$TotBiom[42,2]/mod1$TotBiom_NoFish[42,2]
mod2$TotBiom[42,2]/mod2$TotBiom_NoFish[42,2]
mod3$TotBiom[42,2]/mod3$TotBiom_NoFish[42,2]




